﻿using System.Drawing;
using System.Drawing.Imaging;

namespace PhoobearLibs.Media.Imaging.WBMP
{
    /// <summary>
    /// Wireless Bitmap 확장 
    /// </summary>
    public static class WBMPBitmapExtension
    {
        public static WBMPBitmap Convert(this WBMPBitmap wbmp, Bitmap bitmap)
        {
            try
            {
                //그레이스케일화
                Bitmap grayScaled = wbmp.GrayScaling(bitmap);

                //WBMP구조로 전환 
                int area = grayScaled.Height * grayScaled.Width;
                bool[] bits = new bool[area];

                int index = 0;
                for (int y = 0; y < grayScaled.Height; y++)
                {
                    for (int x = 0; x < grayScaled.Width; x++, index++)
                    {
                        Color pixel = grayScaled.GetPixel(x, y);
                        //int index = (y * bitmap.Width) + x;                     
                        bits[index] = (pixel == Color.Black ? false : true);
                    }
                }

                return new WBMPBitmap() { Type = 0, Size = new Size(bitmap.Width, bitmap.Height), Bitmap = bits };
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return null;
            }
        }

        public static Bitmap GrayScaling(this WBMPBitmap wbmp, Bitmap bitmap)
        {
            Bitmap bm = new Bitmap(bitmap.Width, bitmap.Height);
            Graphics g = Graphics.FromImage(bm);

            ColorMatrix cm = new ColorMatrix(new float[][]{   
                                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                                  new float[]{0,0,0,1,0,0},
                                                  new float[]{0,0,0,0,1,0},
                                                  new float[]{0,0,0,0,0,1}});

            ImageAttributes attribute = new ImageAttributes();
            attribute.SetColorMatrix(cm);

            g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attribute);
            g.Dispose();

            return bm;
        }

        public static Bitmap Convert(this WBMPBitmap wbmp)
        {
            try
            {
                Size size = wbmp.Size;

                Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format1bppIndexed);
                BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

                unsafe
                {
                    int pos = 0;
                    for (int y = 0; y < size.Height; y++)
                    {
                        for (int x = 0; x < size.Width; x++)
                        {
                            byte* p = (byte*)bmpData.Scan0.ToPointer();
                            int index = y * bmpData.Stride + (x >> 3);
                            byte mask = (byte)(0x80 >> (x & 0x7));

                            bool pixel = wbmp.Bitmap[pos];
                            ++pos;

                            if (pixel)
                                p[index] |= mask;
                            else
                                p[index] &= (byte)(mask ^ 0xff);
                        }
                    }
                }

                bitmap.UnlockBits(bmpData);

                return bitmap;
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return null;
            }
        }
    }
}
